/*
 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#ifndef	__DFS_TRANS_RESV_H__
#define	__DFS_TRANS_RESV_H__

struct dfs_mount;

/*
 * structure for maintaining pre-calculated transaction reservations.
 */
struct dfs_trans_res {
	uint	tr_logres;	/* log space unit in bytes per log ticket */
	int	tr_logcount;	/* number of log operations per log ticket */
	int	tr_logflags;	/* log flags, currently only used for indicating
				 * a reservation request is permanent or not */
};

struct dfs_trans_resv {
	struct dfs_trans_res	tr_write;	/* extent alloc trans */
	struct dfs_trans_res	tr_itruncate;	/* truncate trans */
	struct dfs_trans_res	tr_rename;	/* rename trans */
	struct dfs_trans_res	tr_link;	/* link trans */
	struct dfs_trans_res	tr_remove;	/* unlink trans */
	struct dfs_trans_res	tr_symlink;	/* symlink trans */
	struct dfs_trans_res	tr_create;	/* create trans */
	struct dfs_trans_res	tr_mkdir;	/* mkdir trans */
	struct dfs_trans_res	tr_ifree;	/* inode free trans */
	struct dfs_trans_res	tr_ichange;	/* inode update trans */
	struct dfs_trans_res	tr_growdata;	/* fs data section grow trans */
	struct dfs_trans_res	tr_addafork;	/* add inode attr fork trans */
	struct dfs_trans_res	tr_writeid;	/* write setuid/setgid file */
	struct dfs_trans_res	tr_attrinval;	/* attr fork buffer
						 * invalidation */
	struct dfs_trans_res	tr_attrsetm;	/* set/create an attribute at
						 * mount time */
	struct dfs_trans_res	tr_attrsetrt;	/* set/create an attribute at
						 * runtime */
	struct dfs_trans_res	tr_attrrm;	/* remove an attribute */
	struct dfs_trans_res	tr_clearagi;	/* clear agi unlinked bucket */
	struct dfs_trans_res	tr_growrtalloc;	/* grow realtime allocations */
	struct dfs_trans_res	tr_growrtzero;	/* grow realtime zeroing */
	struct dfs_trans_res	tr_growrtfree;	/* grow realtime freeing */
	struct dfs_trans_res	tr_qm_sbchange;	/* change quota flags */
	struct dfs_trans_res	tr_qm_setqlim;	/* adjust quota limits */
	struct dfs_trans_res	tr_qm_dqalloc;	/* allocate quota on disk */
	struct dfs_trans_res	tr_qm_quotaoff;	/* turn quota off */
	struct dfs_trans_res	tr_qm_equotaoff;/* end of turn quota off */
	struct dfs_trans_res	tr_sb;		/* modify superblock */
	struct dfs_trans_res	tr_fsyncts;	/* update timestamps on fsync */
};

/* shorthand way of accessing reservation structure */
#define M_RES(mp)	(&(mp)->m_resv)

/*
 * Per-extent log reservation for the allocation btree changes
 * involved in freeing or allocating an extent.
 * 2 trees * (2 blocks/level * max depth - 1) * block size
 */
#define	DFS_ALLOCFREE_LOG_RES(mp,nx) \
	((nx) * (2 * DFS_FSB_TO_B((mp), 2 * DFS_AG_MAXLEVELS(mp) - 1)))
#define	DFS_ALLOCFREE_LOG_COUNT(mp,nx) \
	((nx) * (2 * (2 * DFS_AG_MAXLEVELS(mp) - 1)))

/*
 * Per-directory log reservation for any directory change.
 * dir blocks: (1 btree block per level + data block + free block) * dblock size
 * bmap btree: (levels + 2) * max depth * block size
 * v2 directory blocks can be fragmented below the dirblksize down to the fsb
 * size, so account for that in the DAENTER macros.
 */
#define	DFS_DIROP_LOG_RES(mp)	\
	(DFS_FSB_TO_B(mp, DFS_DAENTER_BLOCKS(mp, DFS_DATA_FORK)) + \
	 (DFS_FSB_TO_B(mp, DFS_DAENTER_BMAPS(mp, DFS_DATA_FORK) + 1)))
#define	DFS_DIROP_LOG_COUNT(mp)	\
	(DFS_DAENTER_BLOCKS(mp, DFS_DATA_FORK) + \
	 DFS_DAENTER_BMAPS(mp, DFS_DATA_FORK) + 1)

/*
 * Various log count values.
 */
#define	DFS_DEFAULT_LOG_COUNT		1
#define	DFS_DEFAULT_PERM_LOG_COUNT	2
#define	DFS_ITRUNCATE_LOG_COUNT		2
#define DFS_INACTIVE_LOG_COUNT		2
#define	DFS_CREATE_LOG_COUNT		2
#define	DFS_MKDIR_LOG_COUNT		3
#define	DFS_SYMLINK_LOG_COUNT		3
#define	DFS_REMOVE_LOG_COUNT		2
#define	DFS_LINK_LOG_COUNT		2
#define	DFS_RENAME_LOG_COUNT		2
#define	DFS_WRITE_LOG_COUNT		2
#define	DFS_ADDAFORK_LOG_COUNT		2
#define	DFS_ATTRINVAL_LOG_COUNT		1
#define	DFS_ATTRSET_LOG_COUNT		3
#define	DFS_ATTRRM_LOG_COUNT		3

void dfs_trans_resv_calc(struct dfs_mount *mp, struct dfs_trans_resv *resp);

#endif	/* __DFS_TRANS_RESV_H__ */
